//
//  RMainViewController.m
//  StaffTax
//
//  Created by 侯伟 on 2020/2/11.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RMainViewController.h"

#import "RHomeViewController.h"
#import "RDiscoverViewController.h"
#import "RShareViewController.h"
#import "RMineViewController.h"

#import "RNavigationViewController.h"

#import "RUpdateStateRequest.h"
#import "RUpdateStateView.h"

@interface RMainViewController ()

@end

@implementation RMainViewController

- (void)loadView{
    [super loadView];
    
    //去除tabbar黑色线
    self.tabBar.barStyle = UIBarStyleBlack;
    self.tabBar.tintColor = COLOR_THEME;
    self.tabBar.barTintColor = COLOR_WHITE;
    self.tabBar.translucent = NO;
    
    //设置阴影
    self.tabBar.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.tabBar.layer.shadowOffset = CGSizeMake(0, -1);
    self.tabBar.layer.shadowOpacity = 0.3;
    
    
    if (@available(iOS 13.0, *)) {
        UITabBarAppearance *standardAppearance = [UITabBarAppearance new];
        [standardAppearance configureWithOpaqueBackground];
        standardAppearance.backgroundColor = COLOR_WHITE;
        self.tabBar.standardAppearance = standardAppearance;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = standardAppearance;
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadSubControllers];
    
    //请求更新状态
    [Queue delay:3.0 task:^{
        [self requestUpdateState];
    }];
}

- (void)loadSubControllers {
    
    NSMutableArray* tabBarControllers = [NSMutableArray array];

    // 首页
    RHomeViewController* homeController = [RHomeViewController new];
    RNavigationViewController* homeNav = [[RNavigationViewController alloc] initWithRootViewController:homeController];
    UITabBarItem* homeItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"tabbar_home_n"] selectedImage:[UIImage imageNamed:@"tabbar_home_h"]];
    homeItem.tag = 1001;
    homeNav.tabBarItem = homeItem;
    [tabBarControllers addObject:homeNav];


    // 发现
    RDiscoverViewController* discoverController = [RDiscoverViewController new];
    RNavigationViewController* discoverNav = [[RNavigationViewController alloc] initWithRootViewController:discoverController];
    UITabBarItem* discoverItem = [[UITabBarItem alloc] initWithTitle:@"发现" image:[UIImage imageNamed:@"tabbar_discover_n"] selectedImage:[UIImage imageNamed:@"tabbar_discover_h"]];
    discoverItem.tag = 1002;
    discoverNav.tabBarItem = discoverItem;
    [tabBarControllers addObject:discoverNav];

    // 分享
    RShareViewController* shareController = [RShareViewController new];
    RNavigationViewController* shareNav = [[RNavigationViewController alloc] initWithRootViewController:shareController];
    UITabBarItem* shareItem = [[UITabBarItem alloc] initWithTitle:@"分享" image:[UIImage imageNamed:@"tabbar_share_n"] selectedImage:[UIImage imageNamed:@"tabbar_share_h"]];
    shareItem.tag = 1003;
    shareNav.tabBarItem = shareItem;
    [tabBarControllers addObject:shareNav];

    //我的
    RMineViewController* mineController = [RMineViewController new];
    RNavigationViewController* mineNav = [[RNavigationViewController alloc] initWithRootViewController:mineController];

    UITabBarItem* mineItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"tabarl_mine_n"] selectedImage:[UIImage imageNamed:@"tabarl_mine_h"]];
    mineItem.tag = 1004;
    mineNav.tabBarItem = mineItem;
    [tabBarControllers addObject:mineNav];

    [self setViewControllers:tabBarControllers animated:NO];
}

- (void)requestUpdateState{
    RUpdateStateRequest* request = [RUpdateStateRequest new];
    [RequestOperation encryptedRequest:request quite:YES callBack:^(HResponse * _Nullable response) {
        [response success:^(RUpdateState*  _Nonnull data) {
            if(data.status){
                RUpdateStateView* updateView = [RUpdateStateView share];
                updateView.updateState = data;
                [[UIApplication sharedApplication].keyWindow addSubview:updateView];
            }
        }];
    }];
}
@end
